package com.veo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.veo.entity.User;
import com.veo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service
public class UserService extends ServiceImpl<UserMapper, User> {

    @Autowired
    private UserMapper userMapper;

    //验证注册的用户名是否存在
    public String checkUserName(String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){
            //用户不存在
            return "101";
        }else {
            //用户已存在
            return "102";
        }

    }

    /**
     * 登录验证
     * @param loginUser
     * @return
     */
    public String loginCheck(User loginUser, HttpSession session) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("username", loginUser.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){
            //用户不存在
            return "101";
        }else {
            //判断密码
            if (loginUser.getPassword().equals(user.getPassword())){
                session.setAttribute("user",user);
                return "100";
            }else {
                //密码不正确
                return "102";
            }
        }
    }
}
